$(function(){
    $.ajax({
        url: ApiUrl + "/isAuth",
        type: 'GET',
        data: '',
        dataType: 'JSON',
        success: function(result) {
            if(result.code == -200){
                parent.layer.msg(result.message);
                window.location.replace('authLogin.html');
            }
        }
    });
	var admin_id = getCookie('admin_id');
	if(admin_id){
		window.location.replace('index.html');
	}
    $(".m-b").click(function(){
		var phone=$('#phone').val();
		var password=$('#password').val();
        if(!(/^1(3|4|5|7|8)\d{9}$/.test(phone))){ 
	        parent.layer.alert("手机号码不正确，请重填");  
	        return false; 
	    }else if(password.length<6){
        	parent.layer.alert("密码不能小于6位！");  
	        return false; 
        }else{
        	$.ajax({
	    			url: ApiUrl + "/admin/login",
			        type: 'post',
			        data:{
			        	phone : phone,
			        	password : password
			        },
			        dataType: 'JSON',
			        success: function(result) {
			        	if(result.code == 200){
			        		//存储店员信息
			        		addCookie('admin_id',result.data.info.id,72000);
			        		addCookie('admin_uid',result.data.info.iud,72000);
			        		addCookie('real_name',result.data.info.real_name,72000);
			        		addCookie('avatar',result.data.info.avatar,72000);
			        		addCookie('sex',result.data.info.sex,72000);
			        		addCookie('store_id',result.data.info.store_id,72000);
			        		addCookie('status',result.data.info.status,72000);
			        		addCookie('created_at',result.data.info.created_at,72000);
                            addCookie('token',result.token,72000);
			        		//存储菜单列表信息
			        		var menu_list = new Array();
			        		 $.each(result.data.menu, function(k, v) {
			        		 	menu_list[k]=v.id+'/';
			        		 	menu_list[k]+=v.name+'/';
			        		 	menu_list[k]+=v.url+'/';
			        		 	menu_list[k]+=v.icon;
			        		 	if(v.children){
			        		 		$.each(v.children, function(k1, v1) {
			        		 			menu_list[k]+='&'+v1.id+'/';
					        		 	menu_list[k]+=v1.name+'/';
					        		 	menu_list[k]+=v1.url+'/';
					        		 	menu_list[k]+=v1.icon;
			        		 		});
			        		 	}
			        		 });
			        		addCookie('menu',menu_list,72000);
			        		window.location.replace('index.html');
			        	}else{
			        		parent.layer.msg(result.message);
			        		return false;
			        	}
		    		}
	    	});
        }
	});
});